<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Base class of SearchJson.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_jsonaction.php';
require_once INSTALLDIR . '/lib/slh_util.php';


class SearchjsonAction extends JsonAction
{
	/**
	 * Static object?
	 */
	static $staticObj = null;
	
	/**
	 * Query string from the input.
	 */
	var $q;
	
	/**
	 * The returning data items.
	 */
	var $status = '0';			// 0 instruction, 1 success
	var $results = array();		// result array.
	
	/**
	 * Some initializations. Might block this request.
	 */
	function prepare($args)
	{
// TODO: Check the user and the rights.
		parent::prepare($args);
		
		$this->q = $this->getArg('q');
		if (empty($this->q)) {
			return false;
		}
		
		// Inits the jsondata.
		$this->jsondata['status'] = $this->status;
		$this->status =& $this->jsondata['status'];
		
		$this->jsondata['results'] = $this->results;
		$this->results =& $this->jsondata['results'];

		return true;
	}
	
	/**
	 * Handle a request.
	 */
	function handle($args)
	{
		parent::handle($args);
		
		$this->handleQuery();
		
		parent::showJson();
	}
	
	/**
	 * Handle a post, querying the database.
	 * Overriding.
	 */
	function handleQuery()
	{
		/*
		$obj =& self::$staticObj;
		// Get the string.
		$param = strtolower($this->q);
		
		// For 'cnname' or 'pinyin'.
		$query = 'SELECT `user_id`, `cnname` FROM `association` ' .
			   	 'WHERE lower(`pinyin`) LIKE \'' . $param . '%\'' .
			   	 'OR lower(`cnname`) LIKE \'' . $param . '%\'';
		
		$obj->query($query);
		while ($obj->fetch()) {
			$this->results[] = array('name' => $obj->cnname,
									 'id' => $obj->user_id);
		}
		
		// Check if has some results.
		if ( !empty($this->results)) {
			$this->status = '1';
		}
		*/
	}
}

// Static for enficiency.
// Overriding.
SearchjsonAction::$staticObj = null;

?>